//
//  CQHorizontalViewController.m
//  CQPictureBrowser
//
//  Created by 陈长青 on 2017/4/4.
//  Copyright © 2017年 chenchangqing198@126.com. All rights reserved.
//

#import "CQCustomHorizontalViewController.h"
#import "CQVideoPlayerURL.h"
#import "CQVBToolBar.h"
#import "CQVBNavigationBar.h"
#import "CQVideoCell.h"

@interface CQCustomHorizontalViewController ()

@property (strong, nonatomic) NSArray *images;

@end

@implementation CQCustomHorizontalViewController

#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _images = @[@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491373160066&di=95cf9897fcdc0ef8da9ae6c93b470b81&imgtype=0&src=http%3A%2F%2Fimg.bbs.cnhubei.com%2Fforum%2Fdvbbs%2F2004-4%2F200441915031894.jpg",
                @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491366656831&di=d3c290f6ad164471ff29c6f6760812b6&imgtype=0&src=http%3A%2F%2Fpic7.nipic.com%2F20100514%2F2158700_153225558098_2.jpg"
                ,@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491366656831&di=862a94b6842dd02f6c8c9de00acc0de5&imgtype=0&src=http%3A%2F%2Fpic2.ooopic.com%2F12%2F13%2F96%2F45bOOOPICe8_1024.jpg",
                @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491366656831&di=cf6e9203b52d927a1285a438a475c7e3&imgtype=0&src=http%3A%2F%2Fi1.article.fd.zol-img.com.cn%2Ft_s640x2000%2Fg5%2FM00%2F00%2F03%2FChMkJlYp1UeIPe78AAIw4QEANI0AAEAWgMob7oAAjD5701.jpg",
                @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491366656830&di=71dfb666adb5aa58a365cbe044c0ad4c&imgtype=0&src=http%3A%2F%2Fimage89.360doc.com%2FDownloadImg%2F2015%2F10%2F1117%2F60080978_51.jpg",
                @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1491366656830&di=b93cc57816091e6933687bebb88f206a&imgtype=0&src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fphotoblog%2F1206%2F02%2Fc3%2F11848625_11848625_1338612793843.jpg"
                ];
    [self.horizontalView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - CQHorizontalViewDataSource

- (NSInteger)numberOfCellsInHorizontalView:(CQHorizontalView *)horizontalView {
    
    return _images.count+1;
}

- (UIView<CQHorizontalViewCellProtocol> *)horizontalView:(CQHorizontalView *)horizontalView cellForIndex:(NSInteger)index {
    
    UIView<CQHorizontalViewCellProtocol> *cell;
    
    if (index == 0) {
        
        cell = [horizontalView dequeueReusableCellWithIdentifier:@"CQVideoCell"];
        if (cell == nil) {
            cell = [[CQVideoCell alloc] initWithReuseIdentifier:@"CQVideoCell"];
        }
        
        CQVideoCell *videoCell = (CQVideoCell *)cell;
        videoCell.textLabel.text = [NSString stringWithFormat:@"%ld",index];
        CGFloat screenW = CGRectGetWidth([UIScreen mainScreen].bounds);
        CGFloat screenH = CGRectGetHeight([UIScreen mainScreen].bounds);
        [videoCell setPictureSize:CGSizeMake(3, 2) andCellSize:CGSizeMake(MIN(screenH, screenW), MAX(screenW, screenH))];
        [videoCell setRemoteUrl:_images[index+1] andPlaceHolder:[UIImage new]];
        CQVideoPlayer *videoPlayer = [[CQVideoPlayer alloc] initWithVideoPlayerURL:[CQVideoPlayerURL new]];
        videoCell.videoPlayer = videoPlayer;
    } else {
        
        cell = [horizontalView dequeueReusableCellWithIdentifier:@"CQPictureCell"];
        if (cell == nil) {
            cell = [[CQPictureCell alloc] initWithReuseIdentifier:@"CQPictureCell"];
        }
        
        CQPictureCell *pictureCell = (CQPictureCell *)cell;
        pictureCell.textLabel.text = [NSString stringWithFormat:@"%ld",index];
        CGFloat screenW = CGRectGetWidth([UIScreen mainScreen].bounds);
        CGFloat screenH = CGRectGetHeight([UIScreen mainScreen].bounds);
        [pictureCell setPictureSize:CGSizeMake(3, 2) andCellSize:CGSizeMake(MIN(screenH, screenW), MAX(screenW, screenH))];
        [pictureCell setRemoteUrl:_images[index-1] andPlaceHolder:[UIImage new]];
    }
    
    return cell;
}

- (CGFloat)widthForHorizontalView:(CQHorizontalView *)horizontalView {
    
    return [UIScreen mainScreen].bounds.size.width;
}

- (CGFloat)heightForHorizontalView:(CQHorizontalView *)horizontalView {
    
    return [UIScreen mainScreen].bounds.size.height;
}

//- (UIView<CQToolBarProtocol> *)horizontalView:(CQHorizontalView *)horizontalView toolBarForIndex:(NSInteger)index {
//
//    UIView<CQToolBarProtocol> * toolBar;
//    if (index == 0) {
//        
//        toolBar = [horizontalView dequeueReusableToolBarWithIdentifier:@"CQVBToolBar"];
//        if (toolBar == nil) {
//            
//            toolBar = [[CQVBToolBar alloc] initWithReuseIdentifier:@"CQVBToolBar"];
//        }
//    }
//    return toolBar;
//}
//
//- (UIView<CQNavigationBarProtocol> *)horizontalView:(CQHorizontalView *)horizontalView navigationBarForIndex:(NSInteger)index {
//    
//    UIView<CQNavigationBarProtocol> *navigationBar = [horizontalView dequeueReusableNavigationBarWithIdentifier:@"CQVBNavigationBar"];
//    if (navigationBar == nil) {
//        
//        navigationBar = [[CQVBNavigationBar alloc] initWithReuseIdentifier:@"CQVBNavigationBar"];
//    }
//    
//    return navigationBar;
//}

@end
